using Senparc.Weixin.Entities;
using Senparc.Weixin.Helpers;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP.AdvancedAPIs.CustomService;
using Senparc.Weixin.MP.CommonAPIs;
using System;
using System.Collections.Generic;
namespace Senparc.Weixin.MP.AdvancedAPIs
{
	public static class CustomServiceApi
	{
		public static GetRecordResult GetRecord(string accessTokenOrAppId, DateTime startTime, DateTime endTime, string openId = null, int pageSize = 10, int pageIndex = 1, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<GetRecordResult>(delegate(string accessToken)
			{
				string urlFormat = " https://api.weixin.qq.com/customservice/msgrecord/getrecord?access_token={0}";
				if (pageSize <= 0)
				{
					pageSize = 1;
				}
				else
				{
					if (pageSize > 1000)
					{
						pageSize = 1000;
					}
				}
				var data = new
				{
					starttime = DateTimeHelper.GetWeixinDateTime(startTime),
					endtime = DateTimeHelper.GetWeixinDateTime(endTime),
					openId = openId,
					pagesize = pageSize,
					pageIndex = pageIndex
				};
				return CommonJsonSend.Send<GetRecordResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static CustomInfoJson GetCustomBasicInfo(string accessTokenOrAppId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<CustomInfoJson>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token={0}", accessToken);
				return CommonJsonSend.Send<CustomInfoJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static CustomOnlineJson GetCustomOnlineInfo(string accessTokenOrAppId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<CustomOnlineJson>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token={0}", accessToken);
				return CommonJsonSend.Send<CustomOnlineJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult AddCustom(string accessTokenOrAppId, string kfAccount, string nickName, string passWord, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<CustomOnlineJson>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/customservice/kfaccount/add?access_token={0}", accessToken);
				var data = new
				{
					kf_account = kfAccount,
					nickname = nickName,
					password = passWord
				};
				return CommonJsonSend.Send<CustomOnlineJson>(null, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult UpdateCustom(string accessTokenOrAppId, string kfAccount, string nickName, string passWord, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<CustomOnlineJson>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/customservice/kfaccount/update?access_token={0}", accessToken);
				var data = new
				{
					kf_account = kfAccount,
					nickname = nickName,
					password = passWord
				};
				return CommonJsonSend.Send<CustomOnlineJson>(null, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult UploadCustomHeadimg(string accessTokenOrAppId, string kfAccount, string file, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				string url = string.Format("http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token={0}&kf_account={1}", accessToken, kfAccount);
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary["media"] = file;
				return Post.PostFileGetJson<WxJsonResult>(url, null, dictionary, null, null, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult DeleteCustom(string accessTokenOrAppId, string kfAccount, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/customservice/kfaccount/del?access_token={0}&kf_account={1}", accessToken, kfAccount);
				return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, null, CommonJsonSendType.GET, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult CreateSession(string accessTokenOrAppId, string openId, string kfAccount, string text = null, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/customservice/kfsession/create?access_token={0}", accessToken);
				var data = new
				{
					openid = openId,
					kf_account = kfAccount,
					text = text
				};
				return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult CloseSession(string accessTokenOrAppId, string openId, string kfAccount, string text = null, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/customservice/kfsession/close?access_token={0}", accessToken);
				var data = new
				{
					openid = openId,
					kf_account = kfAccount,
					text = text
				};
				return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static GetSessionStateResultJson GetSessionState(string accessTokenOrAppId, string openId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<GetSessionStateResultJson>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/customservice/kfsession/getsession?access_token={0}&openid={1}", accessToken, openId);
				return CommonJsonSend.Send<GetSessionStateResultJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static GetSessionListResultJson GetSessionList(string accessTokenOrAppId, string kfAccount, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<GetSessionListResultJson>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/customservice/kfsession/getsessionlist?access_token={0}&kf_account={1}", accessToken, kfAccount);
				return CommonJsonSend.Send<GetSessionListResultJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static GetWaitCaseResultJson GetWaitCase(string accessTokenOrAppId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<GetWaitCaseResultJson>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/customservice/kfsession/getwaitcase?access_token={0}", accessToken);
				return CommonJsonSend.Send<GetWaitCaseResultJson>(null, urlFormat, null, CommonJsonSendType.GET, timeOut, false);
			}, accessTokenOrAppId, true);
		}
	}
}
